package com.hunlian.makelove.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.R;

public class ShowMsgPopupWindow {
	private Activity context;
	private View conentView;
	public static PopupWindow popupWindow;
	private int delay;

	public ShowMsgPopupWindow(Activity context2, String messageText,
			int messageIcon, int delay) {
		this.context = context2;
		this.delay = delay;

		LayoutInflater inflater = (LayoutInflater) context2
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.show_msg_icon, null);
		((ImageView) conentView.findViewById(R.id.imgMessageIcon))
				.setBackgroundResource(messageIcon);
		((TextView) conentView.findViewById(R.id.txtMessageText))
				.setText(messageText);

		int h = context2.getWindowManager().getDefaultDisplay().getHeight();
		int w = context2.getWindowManager().getDefaultDisplay().getWidth();
		popupWindow = new PopupWindow(conentView, w / 2 + 50,
				LayoutParams.WRAP_CONTENT);
	}

	public ShowMsgPopupWindow(Activity context2, String messageText, int delay) {
		this.context = context2;
		this.delay = delay;

		LayoutInflater inflater = (LayoutInflater) context2
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		conentView = inflater.inflate(R.layout.show_msg_text, null);
		((TextView) conentView.findViewById(R.id.txtMessageText))
				.setText(messageText);

		int h = context2.getWindowManager().getDefaultDisplay().getHeight();
		int w = context2.getWindowManager().getDefaultDisplay().getWidth();
		popupWindow = new PopupWindow(conentView, w / 2 + 50,
				LayoutParams.WRAP_CONTENT);
	}

	public void showPopupWindow(View parent) {
		if (!popupWindow.isShowing()) {
			popupWindow.setBackgroundDrawable(new BitmapDrawable());
			popupWindow.setFocusable(true);
			popupWindow.setOutsideTouchable(false);
			// popupWindow.setAnimationStyle(R.style.AnimationPreview);
			// 刷新状态
			popupWindow.update();
			// popupWindow.showAsDropDown(parent,
			// parent.getLayoutParams().width / 2, 0);
			popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
		}

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				if (popupWindow.isShowing()) {
					popupWindow.dismiss();
				}
			}
		}, delay * 1000);
	}

	public void showPopupWindow() {
		if (!popupWindow.isShowing()) {
			popupWindow.setBackgroundDrawable(new BitmapDrawable());
			popupWindow.setFocusable(true);
			popupWindow.setOutsideTouchable(true);
			// popupWindow.setAnimationStyle(R.style.AnimationPreview);
			// 刷新状态
			popupWindow.update();
			// popupWindow.showAsDropDown(parent,
			// parent.getLayoutParams().width / 2, 0);
			popupWindow.showAtLocation(getRootView(context), Gravity.CENTER, 0,
					0);
		}

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				if (popupWindow.isShowing()) {
					popupWindow.dismiss();
				}
			}
		}, delay * 1000);
	}

	public static final void showIconHint(Activity activity, View parent,
			int icRes, String messageText) {
		new ShowMsgPopupWindow(activity, messageText, icRes, 2)
				.showPopupWindow(parent);
	}

	public static final void showOKHint(Activity activity, View parent,
			String messageText) {
		ShowMsgPopupWindow.showIconHint(activity, parent,
				R.drawable.ic_success, messageText);
	}

	public static final void showQuitHint(Activity activity, View parent,
			String messageText) {
		ShowMsgPopupWindow.showIconHint(activity, parent, R.drawable.ic_quit,
				messageText);
	}

	public static final void showTextHint(Activity activity, View parent,
			String messageText, int delay) {
		new ShowMsgPopupWindow(activity, messageText, delay)
				.showPopupWindow(parent);
	}

	public static final void showTextHint(Activity activity, View parent,
			String messageText) {
		ShowMsgPopupWindow.showTextHint(activity, parent, messageText, 2);
	}

	// 获取activity的根视图（以供居中显示popupWindow）
	private static View getRootView(Activity context) {
		return ((ViewGroup) context.findViewById(android.R.id.content))
				.getChildAt(0);
	}

	public static final void showText(Activity activity, String messageText,
			int delay) {
		new ShowMsgPopupWindow(activity, messageText, delay).showPopupWindow();
	}

	// 推荐的
	public static final void showText(Context activity, String messageText) {
		// XXX 暂时使用Toast替代此方法，等辰显写新的方法来代替
		// ShowMsgPopupWindow.showText(activity, messageText, 2);
		Toast.makeText(activity, messageText, Toast.LENGTH_SHORT).show();

	}

	public static final void showIconText(Activity activity, int icRes,
			String messageText, int delay) {
		new ShowMsgPopupWindow(activity, messageText, icRes, delay)
				.showPopupWindow();
	}

	// 推荐的
	public static final void showIconText(Activity activity, int icRes,
			String messageText) {
		ShowMsgPopupWindow.showIconText(activity, icRes, messageText, 2);
	}

	// 推荐的
	public static final void showOKHint(Activity activity, String messageText) {
		ShowMsgPopupWindow.showIconText(activity, R.drawable.ic_success,
				messageText);
	}

	public static final void showQuitHint(Activity activity, String messageText) {
		ShowMsgPopupWindow.showIconText(activity, R.drawable.ic_quit,
				messageText);
	}
	
	public static void closePopupWindow() {
		if (popupWindow != null && popupWindow.isShowing()) {
			try {
				popupWindow.dismiss();
			} catch (Exception e) {
			}
		}
	}
}
